<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item
              label="标段"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="bidSectionId"
              > 
              <a-select placeholder="请选择标段" v-model="model.bidSectionId">
                <a-select-option v-for="item in exSectionList" :key="item.id" :value="item.id">
                  {{ item.bidSectionName }}
                </a-select-option>
              </a-select>

            </a-form-model-item>
          </a-col> 
          <a-col :span="24">
            <a-form-model-item
              label="标题"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="publicityName"
            >
              <a-input v-model="model.publicityName" placeholder="请输入公示标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="内容"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="publicityContent"
            >
              <j-editor v-model="model.publicityContent" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item
              label="发布媒体"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="noticeMedia"
            >
              <a-input v-model="model.noticeMedia" placeholder="请输入公示发布媒体"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="源url"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="url"
            >
              <a-input v-model="model.url" placeholder="请输入源url"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="publicityType"
            >
              <a-input v-model="model.publicityType" placeholder="请输入类型"></a-input>
            </a-form-model-item>
          </a-col> --> 
          <a-col :span="24">
            <a-form-model-item
              label="开始时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="publicityStartTime"
            >
              <j-date
                placeholder="请选择开始时间"
                v-model="model.publicityStartTime"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="结束时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="publicityEndTime"
            >
              <j-date
                placeholder="请选择结束时间"
                v-model="model.publicityEndTime"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="其他内容"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="otherContent"
            >
              <a-textarea v-model="model.otherContent" rows="4" placeholder="请输入其他内容" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="附件"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="attachmentCode"
            >
              <j-upload v-model="model.attachmentCode"></j-upload>
            </a-form-model-item>
          </a-col> 
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ExTenderCandidateAnnounceForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    projectId: {
      default: '',
    },
    publicityType:{}//公示类型
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        bidSectionId: [{ required: true, message: '请输入标段（包）标识码!' }],
        publicityId: [
          {
            required: true,
            message: '请输入候选人公示标识码 - 交易系统中使用或生成的唯一码,数据交换时新增或修改的依据!',
          },
        ],
        publicityName: [{ required: true, message: '请输入公示标题 - 自由文本!' }],
        publicityContent: [{ required: true, message: '请输入公示内容 - 多个中标候选人一并显示!' }],
        noticeMedia: [{ required: true, message: '请输入公示发布媒体 - 多个以半角分号分割!' }],
        url: [{ required: true, message: '请输入公示源url - 交易平台中标候选人公示的url!' }],
        publicityType: [{ required: true, message: '请输入公示类型 - 参考b.9.6公示类型!' }],
        publicityStartTime: [{ required: true, message: '请输入公示开始时间 - yyyymmddhhmmss!' }],
        publicityEndTime: [{ required: true, message: '请输入公示结束时间 - yyyymmddhhmmss!' }],
        attachmentCode: [{ required: true, message: '请输入附件关联标识号 - 多个附件关联标识号以半角分号隔开!' }],
      },
      url: {
        add: '/exTender/exTenderCandidateAnnounce/add',
        edit: '/exTender/exTenderCandidateAnnounce/edit',
        queryById: '/exTender/exTenderCandidateAnnounce/queryById',
      },
      exSectionList:[],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  watch: {
    projectId: {
      immediate: true,
      handler(val) {
        this.getProjectSection()
      },
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          that.model.publicityType = that.publicityType;
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    getProjectSection() {
      this.exSectionList = []
      getAction('/exProject/exSection/listAll', { projectId: this.projectId })
        .then((res) => {
          if (res.success) {
            var result = res.result
            for (let i = 0; i < result.length; i++) {
              this.exSectionList.push(result[i])
            }
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
  },
}
</script>